//
//	  File.java
//
//	  Copyright (c) 2011 <John Lawniczak>
//	  jlawniczak340license@gmail.com
//
//    This file is part of CS102_Assignment2.
//
//    CS102_Assignment2 is free software: you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation, version 3 of the License.
//
//    CS102_Assignment2 is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with CS102_Assignment2.  If not, see <http://www.gnu.org/licenses/>.
//

/**************************************************
Class Name:File
Author's Name:John Lawniczak
Date:05-04-2010
Description of the class: holds a linked list of 
	lines for each line in the file in memory 
***************************************/
import java.io.FileWriter;
import java.io.PrintWriter;
import java.util.Scanner;
import java.io.IOException;

public class File {
	
	private LinkedList<Line> listOfLines;
	private String fileName ="";
	private PrintWriter fileWriter =null;
	private Scanner fileReader;
	private java.io.File myFile; //this is the jdk version of file so we can use the .exists() to check for preexisting files
	
	
	public File(String fileName){
		this.fileName = fileName;
		listOfLines = new LinkedList<Line>();
		myFile = new java.io.File(fileName);
		try{
			fileWriter = new PrintWriter(new FileWriter(fileName));
			fileReader = new Scanner(new java.io.File(fileName));
		}catch(IOException e){
			e.printStackTrace();
		}
	}

	public String getFileName(){
		return this.fileName;
	}
	
	public String getTextAtLineNum(int index){
		return listOfLines.get(index).getText();
	}
	
	public int getNumLines() {
		return listOfLines.size();
	}
	
	public int getNumWords(){
		String words="";
		for(int i =1; i<=listOfLines.size();i++){
			words = words.concat(listOfLines.get(i).getText() + " ");
		}
		return words.split("\\s").length;
	}
	
	public int getNumChars(){
		String chars="";
		for(int i =1; i<=listOfLines.size();i++){
			chars = chars.concat(listOfLines.get(i).getText());
		}
		return chars.length();
	}
	
	public void addEmptyLine(int index){
		listOfLines.add(index, new Line(""));
	}	
	public void addLine(String lne, int index){
		if(index>listOfLines.size()){
			int delta = index - listOfLines.size()+1;
			for(int i = 1; i<=delta; i++)
				this.addEndLine("");
			this.replaceLine(lne, index);
		}else{
			//this.replaceLine(lne, index);
			listOfLines.add(index-1, new Line(lne));
		}
	}
	
	public void addEndLine(String lne){
		listOfLines.addLast(new Line(lne));
	}
	
	public void addFirstLine  (String lne){
		listOfLines.addFirst(new Line(lne));
	}
	
	public void removeLine(int index){
		listOfLines.remove(index);
	}
	
	public void removeLastLine(){
		listOfLines.removeLast();
	}
	public void removeFirstLine(){
		listOfLines.removeFirst();
	}
	
	public void replaceLine(String lne, int index){
		if(index>listOfLines.size()){
			listOfLines.add(index, new Line(lne));
		}else{
			listOfLines.remove(index);
			listOfLines.add(index-1, new Line(lne));
		}
	}
	
	public void findReplaceInFile(String find, String replace){
		for(int i =1; i<=listOfLines.size();i++){
			listOfLines.get(i).findReplaceAll(find, replace);
		}
	}
	
	public String toString(){
		String ret="";
		for(int i =1; i<=listOfLines.size();i++){
			ret = ret.concat(listOfLines.get(i).getText() + "\r\n");
		}
		return ret;
	}

	public void saveFile(){
		try{
			myFile.delete();
			myFile.createNewFile();
			fileWriter = new PrintWriter(new FileWriter(fileName));
			fileWriter.write(this.toString());
			fileWriter.close();
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	
	public void closeFile(){
		try {
			
			fileWriter.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	
	public void parseFile(){
		try
		{
			if(fileReader.hasNext()){
				listOfLines.addLast(new Line(fileReader.next()));
				System.out.println("line: " + listOfLines.getLast().getText());
			}				
		}catch(Exception e){
			
		}
	}
	
	public Line getFileLine(int i){
		return listOfLines.get(i);
	}
}
